import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useUserStore } from "@/store/user";
import { ElMessage } from "element-plus";
import Home from "@/views/frontend/Home.vue";
import ProductDetail from "@/views/frontend/ProductDetail.vue";
import Layout from "@/views/admin/Layout.vue";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("@/views/frontend/Layout.vue"),
    children: [
      {
        path: "",
        name: "Home",
        component: Home,
        meta: { title: "首页" },
      },
      {
        path: "about",
        name: "About",
        component: () => import("@/views/frontend/About.vue"),
        meta: { title: "关于我们" },
      },
      {
        path: "products",
        name: "Products",
        component: () => import("@/views/frontend/Products.vue"),
        meta: { title: "产品中心" },
      },
      {
        path: "contact",
        name: "Contact",
        component: () => import("@/views/frontend/Contact.vue"),
        meta: { title: "联系我们" },
      },
    ],
  },
  {
    path: "/admin",
    component: Layout,
    children: [
      {
        path: "",
        redirect: "/admin/dashboard",
      },
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/admin/dashboard/Dashboard.vue"),
        meta: { title: "仪表盘", requiresAuth: true },
      },
      {
        path: "users",
        name: "UserManagement",
        component: () => import("@/views/admin/user/UserManagement.vue"),
        meta: { title: "用户管理" },
      },
      {
        path: "settings",
        name: "Settings",
        component: () => import("@/views/admin/Settings.vue"),
        meta: { title: "系统设置" },
      },
      {
        path: "Info",
        name: "Info",
        component: () => import("@/views/admin/Info.vue"),
        meta: { title: "个人信息" },
      },
      {
        path: "products",
        name: "ProductManagement",
        component: () => import("@/views/admin/product/ProductManagement.vue"),
        meta: { title: "产品管理" },
      },
      {
        path: "carousels",
        name: "CarouselManagement",
        component: () =>
          import("@/views/admin/carousel/CarouselManagement.vue"),
        meta: { title: "走马灯管理" },
      },
      {
        path: "website",
        name: "WebsiteManagement",
        component: () => import("@/views/admin/website/websiteManagement.vue"),
        meta: { title: "网站管理" },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/auth/Login.vue"),
    meta: { title: "登录" },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/auth/Register.vue"),
    meta: { title: "注册" },
  },
  {
    path: "/products/:id",
    name: "ProductDetail",
    component: ProductDetail,
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@/views/NotFound.vue"),
    meta: { title: "404" },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const isAuthenticated = !!userStore.token;

  // 设置页面标题
  document.title = `${to.meta.title} - LED Lighting`;

  // 需要认证的路径
  if (to.path.startsWith("/admin") && to.path !== "/login") {
    if (!isAuthenticated) {
      ElMessage.warning("暂无权限！");
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
